package Chatter::API::Controller::Root;
use Moose;
use namespace::autoclean;
use DateTime;
use DateTime::Format::Strptime;

BEGIN { extends 'Catalyst::Controller' }
__PACKAGE__->config(namespace => '');

sub index :Path :Args(0) {
    my ( $self, $c ) = @_;

    # Hello World
    $c->response->body( $c->welcome_message );
}

sub default :Path {
    my ( $self, $c ) = @_;
    $c->response->body( 'Page not found' );
    $c->response->status(404);
}

# 全コントローラ共通前処理
sub begin :Private {
  my ( $self, $c ) = @_;

  # テーブル
  $c->stash->{tAccount} = $c->model('ChatterDB::Account');
  $c->stash->{tThread}  = $c->model('ChatterDB::Thread');
  $c->stash->{tReply}   = $c->model('ChatterDB::Reply');
  $c->stash->{tTweet}   = $c->model('ChatterDB::Tweet');

  # GETリクエスト以外はドロップ
  if($c->req->method ne 'GET'){
    $c->detach;
  }

  # キャッシュ無効化設定
  my $header = $self->genCacheDisableHeader;
  $c->response->header(%$header);
}

sub end : ActionClass('RenderView') {}


# キャッシュ無効用ヘッダ
sub genCacheDisableHeader {
  my ($self) = @_;

  my $dt = DateTime->now(time_zone => 'local');
  my $fmt = DateTime::Format::Strptime->new(pattern => '%a, %d %m %Y %H:%M:%S GMT');
  my $header = {
    'Expires' => 'Sun, 1 Jan 1970 00:00:00 GMT',
    'Last-Modified' => $fmt->format_datetime($dt),
    'Cache-Control' => 'no-store, no-cache, must-revalidate',
    'Cache-Control' => 'post-check=0, pre-check=0',
    'Pragma' => 'no-cache',
  };
  return $header;
}

__PACKAGE__->meta->make_immutable;
1;

